<?php
class ModelCatalogManufacturer extends Model {
	public function addManufacturer($data) {
		if ($data['image']) {
			$data['image'] = html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8');
		}
		$manufacturer_id = $this->db->insert('manufacturer', $data);

		if (isset($data['manufacturer_store'])) {
			foreach ($data['manufacturer_store'] as $store_id) {
				$this->db->query("INSERT INTO  @@manufacturer_to_store SET manufacturer_id = " . (int)$manufacturer_id . ", store_id = " . (int)$store_id);
			}
		}		
	}
	
	public function editManufacturer($manufacturer_id, $data) {
		if ($data['image']) {
			$data['image'] = html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8');
		}
		$this->db->update('manufacturer', $data, "manufacturer_id=$manufacturer_id");
		
		$this->db->query("DELETE FROM  @@manufacturer_to_store WHERE manufacturer_id = " . (int)$manufacturer_id);

		if (isset($data['manufacturer_store'])) {
			foreach ($data['manufacturer_store'] as $store_id) {
				$this->db->query("INSERT INTO  @@manufacturer_to_store SET manufacturer_id = " . (int)$manufacturer_id . ", store_id = " . (int)$store_id);
			}
		}
	}
	
	public function deleteManufacturer($manufacturer_id) {
		$this->db->query("DELETE FROM  @@manufacturer WHERE manufacturer_id = " . (int)$manufacturer_id);
		$this->db->query("DELETE FROM  @@manufacturer_to_store WHERE manufacturer_id = " . (int)$manufacturer_id);
	}	
	
	public function getManufacturer($manufacturer_id) {
		return $this->db->queryOne("SELECT DISTINCT * FROM  @@manufacturer WHERE manufacturer_id = " . (int)$manufacturer_id);
	}
	
	public function getManufacturers($filter = null) {
		$sql = "SELECT * FROM  @@manufacturer";
		
		if ($filter) {
			if ($filter->get('filter_name')) {
				$sql .= " WHERE name LIKE '" . ES($filter->get('filter_name')) . "%'";
			}
			$sql .= $filter->sort('name');
			$sql .= $filter->limit();
		}						
		
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getManufacturerStores($manufacturer_id) {
		return $this->db->queryArray("SELECT store_id FROM  @@manufacturer_to_store WHERE manufacturer_id = " . (int)$manufacturer_id);
	}

	public function getTotalManufacturers() {
      	return $this->db->queryOne("SELECT COUNT(*) AS total FROM  @@manufacturer");
	}	
}
?>